#include "test_task.h"

#include "stdio.h"
#include "string.h"
#include "beep.h"
#include "esp8266.h"
#include "led.h"
#include "stm32f10x.h"

// 从队列接收消息任务
#define RECV_MSG_TASK_PRIO 2
#define RECV_MSG_STK_SIZE  (2*1024)
TaskHandle_t RecvMsgTask_Handler;
void recv_msg_task(void *arg);

extern Data data;	//之前获取的温湿度的数据
int wdHui_flag = 0; //温湿度发送的标志
int light_strong = 0;//光敏电阻发送的标志
int engine_Open = 0; //电机开启标志
int engine_Close = 0;//电机关闭标志

// 定义队列
QueueHandle_t queue1 = NULL;

// 接收消息任务启动函数
void recv_msg_task(void *arg)
{
	while (1) {
		char cmd[256] = "";
		
		// 等待消息
		while (xQueueReceive(queue1, cmd, 10)) {
			printf("xQueueReceive ok\r\n");
			
			if (strstr(cmd, "ORDER:LEDR+OPEN")) {//红灯亮
				LED_R_ON();
			} else if (strstr(cmd, "ORDER:LEDR+CLOSE")) {
				LED_R_OFF();
			} else if (strstr(cmd, "ORDER:LEDG+OPEN")) {//绿灯
				LED_G_ON();
			} else if (strstr(cmd, "ORDER:LEDG+CLOSE")) {
				LED_G_OFF();
			} else if (strstr(cmd, "ORDER:LEDB+OPEN")) {//蓝灯
				LED_B_ON();
			} else if (strstr(cmd, "ORDER:LEDB+CLOSE")) {
				LED_B_OFF();
			} else if (strstr(cmd, "ORDER:TEMP+HUMI")) {//发送温湿度
				wdHui_flag = 1;
			}
			else if (strstr(cmd, "ORDER:SHOW+LIGH")) {//发送光敏电阻
				light_strong = 1;
			}else if (strstr(cmd, "ORDER:ENGI+OPEN")) {//驱动电机开门
				engine_Open = 1;
			}else if (strstr(cmd, "ORDER:ENGI+OFF")) {//驱动电机关门
				engine_Close = 1;
			}
		}
		if (wdHui_flag == 1) { // 检测温度发送标志
			int flag = 0;
			char buf[32] = "";
			sprintf(buf, "DATA:TEMP+%d+humi+%d\r\n", data.tem,data.hum);
			ESP8266_USART_PRINTF("AT+CIPSENDEX=0,%d\r\n", strlen(buf));

			// 等待直到接收到消息
			while (!xQueueReceive(queue1, cmd, 1000)) {
				ESP8266_USART_PRINTF("Failed to receive command\r\n");
				wdHui_flag = 0; // 如果没有接收到消息，则重置发送标志
				break; // 退出函数
			}

			// 检查cmd字符串中是否包含">"
			if (strstr(cmd, ">") != NULL) {
				ESP8266_USART_PRINTF(buf, strlen(buf));
				// 发送数据后，等待确认
				while (!xQueueReceive(queue1, cmd, 1000)) {
					if (strstr(cmd, "OK")) {
						flag = 1;
						break;
					}
				}
				if (flag == 1) {
					wdHui_flag = 0; // 如果接收到"OK"，则重置发送标志
					break;
				}
				wdHui_flag = 0;
			} 
		}
		else if (light_strong == 1)//检测光敏发送的标志
		{
			int flag = 0;
			char buf[32] = "";
			sprintf(buf, "DATA:LIGH+%d\r\n", data.light_strong);
			ESP8266_USART_PRINTF("AT+CIPSENDEX=0,%d\r\n", strlen(buf));

			// 等待直到接收到消息
			while (!xQueueReceive(queue1, cmd, 1000)) {
				ESP8266_USART_PRINTF("Failed to receive command\r\n");
				light_strong = 0; // 如果没有接收到消息，则重置发送标志
				break; // 退出函数
			}

			// 检查cmd字符串中是否包含">"
			if (strstr(cmd, ">") != NULL) {
				ESP8266_USART_PRINTF(buf, strlen(buf));
				// 发送数据后，等待确认
				while (!xQueueReceive(queue1, cmd, 1000)) {
					if (strstr(cmd, "OK")) {
						flag = 1;
						break;
					}
				}
				if (flag == 1) {
					light_strong = 0; // 如果接收到"OK"，则重置发送标志
					break;
				}
				light_strong = 0;
			} 
		}
		else if(engine_Open == 1)		//驱动电机开门
		{
			//发送给门机AT+CIPSENDEX=1,19
//			ESP8266_Send_AT_Cmd("AT+CIPSENDEX=1,17",">",0,500);
//			ESP8266_Send_AT_Cmd("ORDER:ENGI+OPEN","OK",0,1000);
			int flag = 0;
			char *buf = "ORDER:ENGI+OPEN";
			printf("111111111111\r\n");
			ESP8266_USART_PRINTF("AT+CIPSENDEX=1,17\r\n");
			engine_Open = 0; // 如果没有接收到消息，则重置发送标志
			// 等待直到接收到消息
			while (!xQueueReceive(queue1, cmd, 1000)) {
				printf("Failed to receive command\r\n");
				//break; // 退出函数
			}
			
			printf("cmd:%s\r\n", cmd);
			// 检查cmd字符串中是否包含">"
			if (strstr(cmd, ">") != NULL) {
				ESP8266_USART_PRINTF("%s\r\n", buf);
				// 发送数据后，等待确认
				while (!xQueueReceive(queue1, cmd, 1000)) {}
				if (strstr(cmd, "OK")) {
					//engine_Open = 0; // 如果接收到"OK"，则重置发送标志
				}
			}
		}
		else if(engine_Close == 1)		//驱动电机关门
		{
			//发送给门机
			ESP8266_Send_AT_Cmd("AT+CIPSENDEX=1,17",">",0,500);
			ESP8266_Send_AT_Cmd("ORDER:ENGI+OFF","OK",0,500);
		}
	}
}

// 创建接收消息任务
void create_recv_msg_task(void)
{
	xTaskCreate(recv_msg_task, "recv msg task", RECV_MSG_STK_SIZE, NULL, RECV_MSG_TASK_PRIO, &RecvMsgTask_Handler);
}
